package com.hs.hu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.bai.pojo.Order;
import com.hs.bai.service.IOrderService;


import com.hs.ding.pojo.Dispatch;

import com.hs.ding.pojo.Need;
import com.hs.ding.pojo.Transport;
import com.hs.ding.pojo.Vehicle;

import com.hs.ding.service.IDispatchService;
import com.hs.ding.service.INeedService;
import com.hs.ding.service.ITransportService;
import com.hs.ding.service.IVehicleService;
import com.hs.dto.Constants;
import com.hs.dto.ResultBean;
import com.hs.hu.pojo.DispatchReceipt;

import com.hs.hu.pojo.Receipt;
import com.hs.hu.service.IReceiptService;
import com.hs.li.service.ICarManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jiajie Hu
 * @since 2022-06-13
 */
@CrossOrigin
@RestController
@RequestMapping("/receipt")
public class ReceiptController {
    @Autowired
    private IReceiptService receiptService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IVehicleService vehicleService;

    @Autowired
    private ITransportService transportService;

    @Autowired
    private IDispatchService dispatchService;

    @Autowired
    private INeedService needService;

    @Autowired
    private ICarManagementService carManagementService;

    @GetMapping
    public ResultBean queryAllReceiptPage(Integer currentPage,Integer pageSize,String orderId,String customerName){
        return receiptService.queryAllReceiptPage(currentPage,pageSize,orderId,customerName);
//        //查询所有订单
//        List<Order> orders = orderService.list();
//        //创建封装好的回单列表
//        ArrayList<Confirm> confirms = new ArrayList<>();
//        //把派车的属性赋给Confirm类
//        for (Order order : orders) {
//            Confirm confirm = new Confirm();
//            confirm.setOrderId(order.getId());
//            confirm.setOrderType(order.getOrderType());
//            confirm.setTransType(order.getTransType());
//            confirm.setCustomerName(order.getCustomerName());
//            confirm.setOrderTime(order.getOrderTime());
//            confirm.setDepartureCity(order.getDepartureCity());
//            confirm.setDestinationCity(order.getDestinationCity());
//            //查询出该订单对应的派车对象
//            QueryWrapper<DispatchCenter> qr=new QueryWrapper<>();
//            qr.eq("dispatch_order_id",order.getId());
//            DispatchCenter dispatch = dispatchCenterService.getOne(qr);
//            System.out.println("dispatch = " + dispatch);
//            //查询出该派车对象的编号赋于confirm对象。
//            confirm.setDiapatchId(dispatch.getDispatchId());
//            Receipt receipt = receiptService.getById(order.getTRId());
//
//            confirm.setRStatus(receipt.getrStatus());
//            //加入集合
//            confirms.add(confirm);
//        }
//        if(!CollectionUtils.isEmpty(confirms)){
//            ResultBean resultBean = new ResultBean();
//            resultBean.setMsg("success");
//            resultBean.setCode(Constants.SUCCESS_CODE);
//            resultBean.setData(confirms);
//            return resultBean;
//        }
//        return ResultBean.ERROR("failed");
    }

    //绑定订单与回单，创建订单的同时创建回单
    @PostMapping("/{rid}")
    public ResultBean addReceipt(@PathVariable Integer rid){
        QueryWrapper<Order> qr=new QueryWrapper<>();
        qr.eq("t_r_id",rid);
        Order order = orderService.getOne(qr);
        System.out.println("order =--------------- " + order);

        Receipt receipt = new Receipt();
        receipt.setrId(rid);
        receipt.setrStatus(0);
        receipt.setrUpload(0);
        receipt.setOrder(order);
        receipt.setrOrderId(order.getId());
        boolean b = receiptService.saveOrUpdate(receipt);
        if (b){
            return ResultBean.OK();
        }
        return ResultBean.ERROR();
    }
    //通过指定订单id查询回单
     @GetMapping("/{id}")
    public ResultBean getReceipt(@PathVariable String id){
        QueryWrapper<Receipt> qr=new QueryWrapper<>();
        qr.eq("r_order_id",id);
        Receipt receipt = receiptService.getOne(qr);
        System.out.println("receipt =------------------ " + receipt);
        ResultBean resultBean = new ResultBean();
        resultBean.setData(receipt);
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("successful");
        return resultBean;
    }
    //修改回单信息
    @PutMapping()
    public ResultBean editReceipt(@RequestBody Receipt receipt){
        boolean b = receiptService.saveOrUpdate(receipt);
        carManagementService.changeCarTypeByOrderId(receipt.getrOrderId());
        if (b){
            return ResultBean.OK("succeseeful");
        }
        return ResultBean.ERROR("failed");
    }
    @GetMapping("/findNeed/{id}")
    public ResultBean findOrderAndNeed(@PathVariable String id){
        //查询需求单
        Need need = needService.getById(id);
        ArrayList<Need> needs = new ArrayList<>();
        needs.add(need);

        ResultBean resultBean = new ResultBean();
        resultBean.setData(needs);
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("successful");
        return resultBean;
    }

    @GetMapping("/findDispatch/{id}")
    public ResultBean findDispatch(@PathVariable String id){
        //条件查询配车单
        QueryWrapper<Vehicle> qr=new QueryWrapper<>();
        qr.eq("order_id",id);
        Vehicle vehicle = vehicleService.getOne(qr);

        //条件查询需求单
        Need need = needService.getById(id);

        //条件查询运输单
        QueryWrapper<Transport> qw=new QueryWrapper<>();
        qw.eq("v_order_id",vehicle.getvOrderId());
        Transport transport = transportService.getOne(qw);

        //条件查询派车单
        QueryWrapper<Dispatch> qz=new QueryWrapper<>();
        qz.eq("t_order_id",transport.gettOrderId());
        Dispatch dispatch = dispatchService.getOne(qz);

        DispatchReceipt dispatchReceipt = new DispatchReceipt();
        dispatchReceipt.setDispatchId(dispatch.getdOrderId());
        dispatchReceipt.setDispatchRoute(need.getRoute());
        dispatchReceipt.setDispatchCarCarrier(transport.getCarrierName());
        dispatchReceipt.setDispatchCarPlate(transport.getCarPlate());
        dispatchReceipt.setDispatchCarDriver(transport.getDriverName());

        ArrayList<DispatchReceipt> dispatchReceipts = new ArrayList<>();
        dispatchReceipts.add(dispatchReceipt);

        ResultBean resultBean = new ResultBean();
        resultBean.setData(dispatchReceipts);
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("successful");
        return resultBean;
    }

    @GetMapping("/dispatches")
    public ResultBean queryAllDispatchPage(Integer currentPage,Integer pageSize,Integer dispatchId,String dispatchCarPlate,String dispatchCarDriver) {
        return receiptService.queryAllDispatchPage(currentPage, pageSize, dispatchId, dispatchCarPlate,dispatchCarDriver);
    }
}
